package cn.juque.jdfsystem.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.HMac;
import cn.juque.common.base.PageInfo;
import cn.juque.common.constants.MessageEnum;
import cn.juque.common.exception.AppException;
import cn.juque.jdfsystem.config.JdfConfig;
import cn.juque.jdfsystem.entity.UserInfo;
import cn.juque.jdfsystem.enums.JdfSystemMsgEnum;
import cn.juque.jdfsystem.mapper.UserInfoMapper;
import cn.juque.jdfsystem.service.IUserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author JUQUE
 * @version 1.0.0
 * <ul>
 *  <li>simple-generator</li>
 *  <li>用户信息</li>
 * </ul>
 * @date 2023-03-15 17:18:21
 **/
@Service("userInfoService")
public class UserInfoServiceImpl implements IUserInfoService {

    @Resource
    private JdfConfig jdfConfig;

    @Resource
    private UserInfoMapper userInfoMapper;

    /**
     * 分页查询
     *
     * @param pageInfo pageInfo
     * @param userInfo userInfo
     * @return DataGrid
     */
    @Override
    public List<UserInfo> listInfoWithPage(PageInfo pageInfo, UserInfo userInfo) {
        Page<UserInfo> page = new Page<>(pageInfo.getPage(), pageInfo.getLimit());
        List<UserInfo> list = this.userInfoMapper.queryInfoList(page, userInfo);
        pageInfo.setTotal(page.getTotal());
        return list;
    }

    /**
     * 登录
     *
     * @param loginName 登录名
     * @param password  密码
     * @return UserInfo
     */
    @Override
    public UserInfo login(String loginName, String password) {
        Assert.notEmpty(loginName);
        Assert.notEmpty(password);
        UserInfo query = new UserInfo();
        query.setLoginName(loginName);
        List<UserInfo> list = this.userInfoMapper.selectList(new QueryWrapper<>(query));
        if (CollUtil.isEmpty(list)) {
            throw new AppException(JdfSystemMsgEnum.LOGIN_FAIL);
        }
        UserInfo userInfo = list.get(0);
        byte[] rawPassword = userInfo.getPassword().getBytes(StandardCharsets.UTF_8);
        HMac hMac = SecureUtil.hmacMd5(this.jdfConfig.getHmacSecret());
        String targetRaw = hMac.digestHex(password);
        if (!hMac.verify(targetRaw.getBytes(StandardCharsets.UTF_8), rawPassword)) {
            throw new AppException(JdfSystemMsgEnum.LOGIN_FAIL);
        }
        return userInfo;
    }

    /**
     * 查询
     *
     * @param userInfo userInfo
     * @return List
     */
    @Override
    public List<UserInfo> listInfoWithoutPage(UserInfo userInfo) {
        return this.userInfoMapper.queryInfoList(userInfo);
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return UserInfo
     */
    @Override
    public UserInfo detail(String id) {
        return this.userInfoMapper.selectById(id);
    }

    /**
     * 保存
     *
     * @param userInfo userInfo
     */
    @Override
    public void saveInfo(UserInfo userInfo) {
        HMac hMac = SecureUtil.hmacMd5(this.jdfConfig.getHmacSecret());
        String rawPassword = hMac.digestHex(userInfo.getPassword());
        userInfo.setPassword(rawPassword);
        boolean flag = SqlHelper.retBool(this.userInfoMapper.insert(userInfo));
        if (!flag) {
            throw new AppException(MessageEnum.SAVE_FAIL);
        }
    }

    /**
     * 更新
     *
     * @param userInfo userInfo
     */
    @Override
    public void updateInfo(UserInfo userInfo) {
        if(CharSequenceUtil.isNotEmpty(userInfo.getPassword())) {
            HMac hMac = SecureUtil.hmacMd5(this.jdfConfig.getHmacSecret());
            String rawPassword = hMac.digestHex(userInfo.getPassword());
            userInfo.setPassword(rawPassword);
        }
        boolean flag = SqlHelper.retBool(this.userInfoMapper.updateById(userInfo));
        if (!flag) {
            throw new AppException(MessageEnum.UPDATE_FAIL);
        }
    }

    /**
     * 删除
     *
     * @param ids 主键列表
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteInfo(List<String> ids) {
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        int count = this.userInfoMapper.deleteBatchIds(ids);
        if (count != ids.size()) {
            throw new AppException(MessageEnum.DELETE_FAIL);
        }
    }
}
